package com.yyj.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yyj.domain.Goods;
import com.yyj.domain.Marange;
import com.yyj.domain.Order;
import com.yyj.domain.User;
import com.yyj.domain.WaitPay;
import com.yyj.service.UserService;

@Controller
public class UserController {

	@Autowired
	private UserService userService;

	
	@RequestMapping("/register.do")
	public String register() {
		return "register";
	}

	
	@RequestMapping(value = "/login.do", method = RequestMethod.GET)
	public String toLogin() {
		return "login";
	}

	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public String login(String account,String password, Model model,HttpServletRequest request, HttpSession session) {
		boolean flag = userService.login(account, password);
		if (flag) {
			User user = userService.getUserByaccount(account);
			session.setAttribute("user", user);
			return "redirect:home.do";
		} else {
			model.addAttribute("msg", "账号或密码错误，请重新输入！");
			return "login";
		}

	}

	//转跳购物车
	@RequestMapping(value = "/gwc.do")
	public String gwc() {
		return "redirect:/getAllCarts.do";
	}
	
	@RequestMapping(value ="/zf.do")
	public String zf() {
		return "zf";
	}
	
	@RequestMapping(value = "/zfcg.do")
	public String zfcg() {
		return "zfcg";
	}
 //注册用户
	@RequestMapping(value="/createUser.do" ,method=RequestMethod.POST)
	@ResponseBody
	public User userCreate(@RequestBody User user) {
		System.out.println(user);
		int n = userService.createUser(user);
		if (n > 0) {
			System.out.println(user);
		}
		return user;
	}
	//我的页面
	@RequestMapping("/my.do")
	public String my() {
		return "redirect:/selewapa.do";
	}
	//查询个人相关信息
	@RequestMapping(value = "/selewapa.do")
	public String selewapa(Model model,HttpSession session) {
		User users=(User)session.getAttribute("user");
		List<WaitPay> waitPay=userService.selectWaitPay(users.getNickname());
		List<Order> order=userService.selectOrder(users.getNickname());
		model.addAttribute("waitPay", waitPay);
		model.addAttribute("order", order);
		return "my";
	}
	//修改信息
	@RequestMapping("/updateuser.do")
	public String updatauser(User user,HttpServletRequest request) {
		userService.updateUser(user);
		User users = userService.getUserByaccount(user.getAccount());
		request.getSession().setAttribute("user", users);
		return "redirect:/selewapa.do";
	}
	//立即支付
	@RequestMapping("/userwp.do")
	public String selectwap(Integer  waitpay_id,Model model) {
		WaitPay wapPay=userService.selectMyPAy(waitpay_id);
		model.addAttribute("waitpay", wapPay);
		return "balance";
	}
	//再次购买
	@RequestMapping("/userorderbuy.do")
	public String selectuserorderbuy(String goods_id,Model model) {
		Goods goods=userService.selectMyBuy(goods_id);
		model.addAttribute("goods", goods);
		return "goods";
	}
	//从待支付表中删除
  @RequestMapping("/delectdzf.do")
  public String delectdzf(Integer waitpay_id,Model model,HttpServletRequest request) {
	  userService.delectwpuser(waitpay_id);
	User user=  (User) request.getSession().getAttribute("user");
	User users=userService.getUserByaccount(user.getAccount());
	request.getSession().setAttribute("user", users);
	return "redirect:/selewapa.do";
  }
 //订单表中删除
  @RequestMapping("/deletedanuser.do")
  public String deletedanuser(Integer order_id,HttpServletRequest request) {
	  userService.delectorderuser(order_id);
	  User user=  (User) request.getSession().getAttribute("user");
		User users=userService.getUserByaccount(user.getAccount());
		request.getSession().setAttribute("user", users);
		return "redirect:/selewapa.do";
  }
  
	//退出登录
	@RequestMapping("/logout.do")
	public String logout(HttpSession session) {
		//清除Session
		session.invalidate();
		//重定向到登录页面的转跳放法
		return "redirect:login.do";
	}
	
	
	
	
	
	
	@RequestMapping("/login2.do")
	public String tologin2() {
		return "login2";
	}
	@RequestMapping("/mlogin.do")
	public String mlogin(Marange marange) {
		Marange marange2=userService.selectMarange(marange);
		if(marange2!=null) {
			return "redirect:/marshowgoods.do";
		}
		return "login2";
	}
}
